{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 列表  元组  字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#列表的创建\n",
    "#1 range\n",
    "list(range(6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#2推导式\n",
    "[i for i in range(6)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "列表中所保存的是对象的指针，其中一个元素发生改变，引用此地址的数据也将发生改变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "严格来说 append 是追加，把一个东西整体添加在列表后，而 extend 是扩展，把一个东西里的所有元素添加在\n",
    "列表后。append适合添加一个元素，extend适合一次性添加多个元素。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    " list.insert(index, obj) 在编号 index 位置前插入 obj "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list.remove()移除remove中指定的元素，列表中如有重复项，则移除第一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    " list.pop([index=-1]) 移除列表中的一个元素（默认最后一个元素），并且返回该元素的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Wednesday', 'Thursday', 'Friday']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#如果知道要删除的元素在列表中的位置，可使用 del 语句\n",
    "x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']\n",
    "del x[0:2]\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Friday', 'Thursday', 'Wednesday', 'Tuesday', 'Monday']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']\n",
    "week[::-1] #快速将列表元素倒置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[123, 456, 789, 213]\n",
      "[123, 456, 789, 213]\n",
      "[123, 213, 456, 789]\n",
      "[123, 456, 789, 213]\n"
     ]
    }
   ],
   "source": [
    "list1 = [123, 456, 789, 213]\n",
    "list2 = list1\n",
    "list3 = list1[:]\n",
    "print(list2) \n",
    "print(list3) \n",
    "list1.sort()\n",
    "print(list2) # 传的是指针\n",
    "print(list3) # 值已经拷贝过去，不在用指针指向原列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[123, 456, 123, 456, 123, 456]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list1 = [123, 456]\n",
    "list2 = [456, 123]\n",
    "list1 + list2 #同extend的作用\n",
    "list3 = list1*3\n",
    "list3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#list.count() 统计某个元素在列表中出现的次数\n",
    "list3.count(123) #里面必须要指定元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#list.index(x[, start[, end]]) 从列表中找出某个值第一个匹配项的索引位置\n",
    "list3.index(123)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#疑问\n",
    "list1.reverse()==list1[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[789, 456, 123]\n"
     ]
    }
   ],
   "source": [
    "x = [123, 456, 789]\n",
    "x.reverse()#该方法没有返回值\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[789, 456, 213, 123]\n"
     ]
    }
   ],
   "source": [
    "x = [123, 456, 789, 213]\n",
    "x.sort(reverse=True)\n",
    "print(x) #该方法没有返回值，但是会对列表对象排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "练习题1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "lst = [2, 5, 6, 7, 8, 9, 2, 9, 9]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 5, 6, 7, 8, 9, 2, 9, 9, 15]\n"
     ]
    }
   ],
   "source": [
    "lst.append(15)\n",
    "print(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 5, 6, 7, 8, 20, 9, 2, 9, 9, 15]\n"
     ]
    }
   ],
   "source": [
    "lst.insert(5,20)\n",
    "print(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "lst.extend([2,5,6])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 5, 6, 7, 8, 20, 9, 2, 9, 9, 15, 2, 5, 6]"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst.pop(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6, 5, 2, 15, 9, 9, 2, 9, 20, 8, 6, 5, 2]\n"
     ]
    }
   ],
   "source": [
    "lst.reverse()\n",
    "print(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 2, 2, 5, 5, 6, 6, 8, 9, 9, 9, 15, 20]\n"
     ]
    }
   ],
   "source": [
    "lst.sort()\n",
    "print(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[20, 15, 9, 9, 9, 8, 6, 6, 5, 5, 2, 2, 2]\n"
     ]
    }
   ],
   "source": [
    "lst.sort(reverse=True)\n",
    "print(lst)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "练习二"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [],
   "source": [
    "lst = [1, [4, 6], True]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "lst[0] = lst[0]*2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "lst[1][0] = lst[1][0]*2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [],
   "source": [
    "lst[1][1] = lst[1][1]*2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, [8, 12], True]"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "元组在创建之后就不能对其修改\n",
    "创建元组最好还是使用()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 10.31, 'python') <class 'tuple'>\n",
      "(1, 10.31, 'python') <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "t1 = (1, 10.31, 'python')\n",
    "t2 = 1, 10.31, 'python'\n",
    "print(t1, type(t1))\n",
    "print(t2, type(t2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    " #元组中只包含一个元素时，需要在元素后面添加逗号，否则括号会被当作运算符使用：\n",
    "temp = (1,)\n",
    "print(type(temp))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "不可以直接给元组赋值，但是可以更改元组中的元素的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2, 3, 4, 5, '老马的程序人生', '小马的程序人生')\n"
     ]
    }
   ],
   "source": [
    "# + 同列表一样\n",
    "t1 = (2, 3, 4, 5)\n",
    "t2 = ('老马的程序人生', '小马的程序人生')\n",
    "t3 = t1 + t2\n",
    "print(t3) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = (1, 10.31, 'python')\n",
    "t.index(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [],
   "source": [
    "#解压一维元组 有几个元素左边括号中定义几个变量\n",
    "t = (1, 10.31, 'python')\n",
    "(a, b, c) = t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10.31"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 10.31 OK python\n"
     ]
    }
   ],
   "source": [
    "#解压二维元组（按照元组里的元组结构来定义变量）\n",
    "t = (1, 10.31, ('OK', 'python'))\n",
    "(a, b, (c, d)) = t\n",
    "print(a, b, c, d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 5\n"
     ]
    }
   ],
   "source": [
    "#只想要元组其中几个元素，用通配符「*」\n",
    "t = 1, 2, 3, 4, 5\n",
    "a, b, *rest, c = t\n",
    "print(a, b, c) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "练习题一"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 1, 2) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "a = (1, 2)*2\n",
    "print(a,type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 1) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "a = (1, )*2\n",
    "print(a,type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 <class 'int'>\n"
     ]
    }
   ],
   "source": [
    "a = (1)*2\n",
    "print(a,type(a))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "练习题二"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "不属于，只是简单赋值\n",
    "按正则匹配的方式？"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "let\n",
      "'s go\n"
     ]
    }
   ],
   "source": [
    "#如果字符串中需要出现单引号或双引号，可以使用转义符号 \\ 对字符串中的符号进行转义 \\n换行 \\r回车  \\t制表符 代表一个tab\n",
    "print('let\\n\\'s go')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这是一个多行字符串的实例\n",
      "多行字符串可以使用制表符\n",
      "TAB ( \t \t \t )。\n",
      "也可以使用换行符 [ \n",
      " ]。\n",
      "\n"
     ]
    }
   ],
   "source": [
    "para_str = \"\"\"这是一个多行字符串的实例\n",
    "多行字符串可以使用制表符\n",
    "TAB ( \\t \\t \\t )。\n",
    "也可以使用换行符 [ \\n ]。\n",
    "\"\"\"\n",
    "print (para_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "capitalize() 将字符串的第一个字符转换为大写\n",
    "lower() 转换字符串中所有大写字符为小写。\n",
    "upper() 转换字符串中的小写字母为大写。\n",
    "swapcase() 将字符串中大写转换为小写，小写转换为大写。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "1. endswith(suffix, beg=0, end=len(string)) 检查字符串是否以指定子字符串 suffix 结束，如果是，\n",
    "返回 True,否则返回 False。如果 beg 和 end 指定值，则在指定范围内检查。\n",
    "2. startswith(substr, beg=0,end=len(string)) 检查字符串是否以指定子字符串 substr 开头，如果\n",
    "是，返回 True，否则返回 False。如果 beg 和 end 指定值，则在指定范围内检查。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "str2 = \"DAXIExiaoxie\"\n",
    "print(str2.count('xi',0,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "查找函数\n",
    "1. find(str, beg=0, end=len(string)) 检测 str 是否包含在字符串中，如果指定范围 beg 和 end ，则\n",
    "检查是否包含在指定范围内，如果包含，返回开始的索引值，否则返回 -1。\n",
    "2. rfind(str, beg=0,end=len(string)) 类似于 find() 函数，不过是从右边开始查找"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "str2 = \"DAXIExiaoxie\"\n",
    "print(str2.find('xi'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "isnumeric() 如果字符串中只包含数字字符，则返回 True，否则返回 False。\n",
    "ljust(width, fillchar) 返回一个原字符串左对齐，并使用 fillchar （默认空格）填充至长度 width\n",
    "的新字符串。rjust达到相反的效果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1101aaaa\n",
      "aaaa1101\n"
     ]
    }
   ],
   "source": [
    "str4 = '1101'\n",
    "print(str4.ljust(8, 'a')) \n",
    "print(str4.rjust(8, 'a')) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lstrip（）默认去除字符串最左边的空格，也可以指定去除哪个字符，请注意，去除并不是替换,并且去除的字符也是最右边或最左边的\n",
    "strip（）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I Love LsgoGroup           \n",
      " Love LsgoGroup           \n",
      "          I Love LsgoGroup\n",
      "I Love LsgoGroup\n",
      "I Love LsgoGrou\n"
     ]
    }
   ],
   "source": [
    "str5 = '          I Love LsgoGroup           '\n",
    "print(str5.lstrip()) \n",
    "print(str5.lstrip().strip('I')) \n",
    "print(str5.rstrip()) \n",
    "print(str5.strip()) \n",
    "print(str5.strip().strip('p'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "replace(old, new [, max]) 把 将字符串中的 old 替换成 new ，如果 max 指定，则替换不超过 max 次。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "We I Love LsgoGroup\n"
     ]
    }
   ],
   "source": [
    "str5 = ' I I Love LsgoGroup '\n",
    "print(str5.strip().replace('I', 'We',1)) # We Love LsgoGroup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "partition(sub) 找到子字符串sub，把字符串分为一个三元组 (pre_sub,sub,fol_sub) ，如果字符串中不包含sub则返回 ('原字符串','','') 。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('I L', 'o', 've LsgoGroup')\n",
      "('I Love LsgoGroup', '', '')\n"
     ]
    }
   ],
   "source": [
    "str5 = ' I Love LsgoGroup '\n",
    "print(str5.strip().partition('o')) \n",
    "print(str5.strip().partition('m')) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('I L', 'o', 've LsgoGroup')\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'I L'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str6 = str5.strip().partition('o')\n",
    "print(str6)\n",
    "str6[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "split(str=\"\", num) 不带参数默认是以空格为分隔符切片字符串，如果 num 参数有设置，则仅分隔 num 个子字符串，返回切片后的子字符串拼接的列表\n",
    "可以在分割的时候直接找到变量进行接收\n",
    "在分割的时候会把用于分割的字符串剔除，将其与的内容转换成列表的形式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['I', 'Love', 'LsgoGroup']\n",
      "['I Love L', 'goGroup']\n"
     ]
    }
   ],
   "source": [
    "str5 = ' I Love LsgoGroup '\n",
    "print(str5.strip().split())\n",
    "print(str5.strip().split(\"s\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['www.baidu.com.cn']\n",
      "['www', 'baidu.com.cn']\n",
      "['www', 'baidu', 'com.cn']\n"
     ]
    }
   ],
   "source": [
    "u = \"www.baidu.com.cn\"\n",
    "print(u.split(\".\",0))\n",
    "print(u.split(\".\",1))\n",
    "print(u.split(\".\",2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'www'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "u1, u2, u3 = u.split(\".\", 2)\n",
    "u1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "str6 = 'I \\n Love \\n LsgoGroup'\n",
    "print(str6.splitlines()) # ['I ', ' Love ', ' LsgoGroup']\n",
    "print(str6.splitlines(True)) # ['I \\n', ' Love \\n', ' LsgoGroup']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "字符串格式化\n",
    "1.format格式化函数\n",
    "2.字符串格式化符号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'I Love Lsgogroup'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str = \"{0} Love {1}\".format('I', 'Lsgogroup') #位置参数\n",
    "str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'I Love Lsgogroup'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str = \"{a} Love {b}\".format(a='I',b='Lsgogroup') #关键字参数\n",
    "str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I Love Lsgogroup\n"
     ]
    }
   ],
   "source": [
    "str = \"{0} Love {b}\".format('I', b='Lsgogroup') # 位置参数要在关键字参数之前\n",
    "print(str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "27.6580GB\n"
     ]
    }
   ],
   "source": [
    "str = '{0:.4f}{1}'.format(27.658, 'GB') # 保留小数点后四位\n",
    "print(str) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n"
     ]
    }
   ],
   "source": [
    "print('%c' % 97) #格式化字符及ASCII码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4 + 5 = 9\n"
     ]
    }
   ],
   "source": [
    "print('%d + %d = %d' % (4, 5, 9))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我叫 小明 今年 10 岁!\n"
     ]
    }
   ],
   "source": [
    "print(\"我叫 %s 今年 %d 岁!\" % ('小明', 10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "27.7  \n"
     ]
    }
   ],
   "source": [
    "print('%-6.1f' % 27.658)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  27.7\n"
     ]
    }
   ],
   "source": [
    "print('%6.1f' % 27.658)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "练习题一：\n",
    "str.replace\n",
    "str.split()\n",
    "str.lsplit() str.rsplit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "练习题二：\n",
    "实现函数isdigit， 判断字符串里是否只包含数字0~9\n",
    "isnumberic判断字符串中是否只包含数字字符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "字符串只包含数字\n"
     ]
    }
   ],
   "source": [
    "def isdigit(string):\n",
    "    for i in string:\n",
    "        flag = True\n",
    "        if i in ['0','1','2','3','4','5','6','7','8','9']:\n",
    "            flag = True\n",
    "        else:\n",
    "            flag = False\n",
    "            print(\"字符串不只包含数字\")\n",
    "            break\n",
    "    if flag == True:\n",
    "        print(\"字符串只包含数字\")\n",
    "isdigit('256')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
